/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.preferences;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

/**
 *
 * @author Kenneth
 */
public class UserTaxonomyPreferences extends Properties {

    private final String propertiesFileName = "taxonomy.properties";
    private final File propertiesFile;
    private static UserTaxonomyPreferences instance = null;

    public static UserTaxonomyPreferences getInstance() throws IOException {
        if (instance == null) {
            instance = new UserTaxonomyPreferences();
        }
        return instance;
    }

    private UserTaxonomyPreferences() throws IOException {
        propertiesFile = new File(getCurrentJarLocation(), propertiesFileName);
        InputStream inputStream;
        if (propertiesFile.exists()) {
            inputStream = new FileInputStream(propertiesFile);
        } else {
            inputStream = this.getClass().getClassLoader()
                    .getResourceAsStream(propertiesFile.getName());
        }
        load(inputStream);
    }

    private File getCurrentJarLocation() throws UnsupportedEncodingException {
        String path = UserTaxonomyPreferences.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        return new File(URLDecoder.decode(path, "UTF-8"));
    }

    public void saveOrganism(String anOrganismName) {
        setProperty(anOrganismName, "saved");
    }

    public void removeOrganism(String anOrganismName) {
        remove(anOrganismName);
    }

    public void saveToFile() throws IOException {
        store(new FileWriter(propertiesFile), "List of selected (model)organisms");
    }

}
